package com.javen.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.javen.model.User;
import com.javen.service.UserService;

@Controller//默认情况下返回视图
@RequestMapping("/user")
public class userController {
	@Autowired
	private UserService userService;	
	@RequestMapping(value="/login", method=RequestMethod.GET, produces = "text/plain;charset=utf-8")
	@ResponseBody
	public String login(HttpServletRequest request) {
		
		String userNameString=request.getParameter("userName");
		String passwordString=request.getParameter("password");


		System.out.println(userNameString+","+passwordString);
		User user =new User();
		user.setPassword(passwordString);
		user.setUserName(userNameString);
		HttpSession session=request.getSession();
		session.setAttribute("carOwner",userNameString);
		System.out.println(session.getAttribute("carOwner"));
		boolean flage=userService.login(user);
		String jsonString="";
		if(flage) {
			jsonString="{\"message\":\"成功\",\"userName\":\""+ userNameString +"\"}";
		}else {
			jsonString="{\"message\":\"失败\"}";
		}
		return jsonString;
	}
	
	@RequestMapping("/insert")
	@ResponseBody

	public int insert(String userName,String password) {
		//String userNameString=request.getParameter("userName");
		//String passwordString=request.getParameter("password");
		System.out.println(userName+""+password);
		User user=new User();
		user.setUserName(userName);
		user.setPassword(password);

		int code=userService.insert(user);
		return code;
	}
	
		
	}
	
	
	
	
	

 